<?php $this->load->view('header');?>

<body class="childrenBody custom_body">
	<div class="custom_main">
		<form class="layui-form">
			<div class="layui-form-item">
				<label class="layui-form-label">头像：</label>
				<div class="layui-upload">
				  <button type="button" class="layui-btn layui-btn-primary" id="test1">上传</button>
				  <div class="layui-upload-list" style="display: inline;">
				    <img class="layui-upload-img" id="demo1">
				    <p id="demoText" style="display: inline;color: #ccc">建议正方形图片，大小不超过2M。支持格式jpg、jpeg、gif、png</p>
				  </div>
				  <input type="hidden" name="headimg" value="<?=$headimg?>" />
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">账户名</label>
				<div class="layui-input-block">
					<input type="text" name="name" class="layui-input userName" lay-verify="required" placeholder="请输入登录名" value="<?=$name?>">
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-inline">
		   			<label class="layui-form-label">登录密码</label>
		    		<div class="layui-input-inline">
		      			<input type="password" name="password" <?php if ($admin_id <= 0):?> lay-verify="required"<?php endif;?> placeholder="请输入密码" autocomplete="off" class="layui-input">
		    		</div>
				</div>
				<div class="layui-inline">
		   			<label class="layui-form-label">确认密码</label>
		    		<div class="layui-input-inline">
		      			<input type="password" name="confirm_password" <?php if ($admin_id <= 0):?> lay-verify="required" <?php endif;?> placeholder="请输入确认密码" autocomplete="off" class="layui-input">
		    		</div>
				</div>
			</div>

			<div class="layui-form-item layui-form-text">
			    <label class="layui-form-label">备注</label>
			    <div class="layui-input-block">
			      <textarea placeholder="请输入内容" name="remarks" class="layui-textarea"><?=$remarks?></textarea>
			    </div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<input type="hidden" name="admin_id" value="<?=$admin_id?>" />
					<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addUser">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			    </div>
			</div>
		</form>
	</div>

	<!-- 模板 -->
	<script id="demo" type="text/html">
		<select name="role_id" lay-verify="required">
		<option value="">请选择一个角色</option>
	    {{#  layui.each(d.list, function(index, item){ }}
			<option value="{{ item.role_id }}">{{ item.role_name }}</option>
	    {{#  }); }}

	    {{#  if(d.list.length === 0){ }}
	      无数据
	    {{#  } }} 
	    </select>
	</script>

	<script type="text/javascript">
		layui.config({
			version: true
		}).use(['jquery','layer', 'form', 'laytpl', 'upload'],function(){
			var form = layui.form,
				layer = parent.layer === undefined ? layui.layer : parent.layer,
				laypage = layui.laypage,
				laytpl = layui.laytpl,
				upload = layui.upload,
				$ = layui.jquery;

				var aid = <?=$admin_id?>;
				var image_load = '<?=$image_upload_url?>';

				//上传图片
				var uploadInst = upload.render({
				    elem: '#test1'
				    ,url: image_load
				    ,before: function(obj){
				      //预读本地文件示例，不支持ie8
				      obj.preview(function(index, file, result){
				        $('#demo1').attr('src', result); //图片链接（base64）
				        $("#demo1").css("width","100px");
				      });
				    }
				    ,done: function(res){
				      //如果上传失败
				      if(res.result_code > 0){
				        return layer.msg('上传失败');
				      } 
				      //上传成功
				      else{
				      	$("input[name='headimg']").val(res.data[0].url);
				      }
				     
				    }
				    ,error: function(){
				      //演示失败状态，并实现重传
				      var demoText = $('#demoText');
				      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
				      demoText.find('.demo-reload').on('click', function(){
				        uploadInst.upload();
				      });
				    }
				  });

				//提交表单
				form.on("submit(addUser)",function(data)
				{

					if (data.field.password.length > 0 || aid <= 0) 
					{
						if (data.field.password.length < 6){
							layer.msg('密码长度不得少于6个字符！', {icon: 5});
							return false;
						}

						if (data.field.password != data.field.confirm_password) {
							layer.msg('密码不一致！', {icon: 5});
							return false;
						}
					}


					$.post('admin.php/AdminUser/insert_admin_user', data.field, function(result) {
						if (result.status == 200) 
						{
							layer.msg(result.message, {icon: 1});
							location.reload();
						}
						else
						{
							layer.msg(result.message, {icon: 5});
						}

					},"json");

					return false;
				});



		});

	</script>

</body>
</html>